package com.sz.jsoup;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class Biquge {

    //查询 百度
    private static Logger logger = Logger.getLogger(Biquge.class);
    private static final String BASIC_URL = "http://www.xxbiquge.com";

    public static Document docBuilderPOST(String url) {
        Document doc = null;
        try {
            doc = Jsoup.connect(url)
                    .userAgent("Mozilla")
                    .timeout(3000)
                    .post();

            return doc;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doc;
    }

    public static Document docBuilderGET(String url) {
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();

            return doc;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doc;
    }

    public static void readMenu() {
        Document doc = docBuilderPOST(BASIC_URL + "/0_114");
        Element menu = doc.body().getElementById("list");
        Elements menuNames = menu.getElementsByTag("a");
        //        StringBuffer str = new StringBuffer();
        int i = 1;
        for (Element menuName : menuNames) {
            //            logger.info(menuName.text() + "---" + BASIC_URL + menuName.attr("href"));

            writerTxT(menuName.text(), BASIC_URL + menuName.attr("href"));
            logger.info("第" + i + "章写入完成。");
            i++;

            //            str.append(menuName.text());
            //            str.append("\n");
            //            if(i < menuNames.size() || i % 100 == 0){
            //                i++;
            //                continue;
            //            }
            //            WriterTXT.wirterTXT(DIR_NAME, FILE_NAME, str.toString(), true);
            //            str.setLength(0);
        }
    }

    public static void writerTxT(String title, String url) {
        Document doc = docBuilderGET(url);
        Element contentElement = doc.body().getElementById("content");
        StringBuffer str = new StringBuffer(title);
        str.append(System.getProperty("line.separator"));
        String content = contentElement.text().replaceAll("    ", System.getProperty("line.separator"));
        str.append(content);
        System.out.println(str.toString());
    }

    public static void main(String[] args) {
        readMenu();
    }
}
